﻿using ConfExpert.Forms.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class HareketsizAksesuar : XForm
    {
        public HareketsizAksesuar()
        {
            InitializeComponent();
        }

        private void HareketsizAksesuar_Load(object sender, EventArgs e)
        {
            
            DataSet d = ExecuteDataSet("GetHareketsizAksesuar", WhoAmI);
            DataTable dtAksBased = d.Tables[0];
            DataTable dtCustBased = d.Tables[1];
            DataTable dtCtgBased = d.Tables[2];
            DataTable dtCustCtgBased = d.Tables[3];

            gvAksBased.Init(dtAksBased);
            gvAksBased.MakeReadOnly();
            gvAksBased.FormatAsDate("SonHareket");
            gvAksBased.FormatAsNumber(0, "Gelen", "Cikan", "Stok");
            gvAksBased.MakeColumnInvisible("SonNotId", "StateId");
            gvAksBased.AddSummary("Stok");
            gvAksBased.DataTable().PrimaryKey = new DataColumn[] { gvAksBased.DataTable().Columns["AksesuarId"] };
            gvAksBased.DisableRowDelete();
            gvAksBased.SetFilterTypeToCheckedList("SonNot");

            gvCustBased.Init(dtCustBased);
            gvCtgBased.Init(dtCtgBased);
            gvCustCtgBased.Init(dtCustCtgBased);
            gvCustBased.MakeReadOnly();
            gvCtgBased.MakeReadOnly();
            gvCustCtgBased.MakeReadOnly();

            GridManager gm = GridManager.GetManager(gvAksBased);
            gm.GridMenu.AddMenu("Değişiklik Notu Ekle", AddNote_Click);
            gm.GridMenu.AddMenu("Stok Hareketleri", gvAksBased_DoubleClick);

            gvAksBased.AddSummary("Stok");
            gvCustBased.AddSummary("Stok");
            gvCtgBased.AddSummary("Stok");
            gvCustCtgBased.AddSummary("Stok");

            gvAksBased.BestFitColumns();
            gvCustBased.BestFitColumns();
            gvCtgBased.BestFitColumns();
            gvCustCtgBased.BestFitColumns();

            gvAksBased.FormatAsNumber(0, "NotSayisi");
            gvAksBased.Columns["NotSayisi"].ColumnEdit = PasifOnay.HyperLink();

            DevExpress.Skins.Skin GridSkin = DevExpress.Skins.GridSkins.GetSkin(this.LookAndFeel.ActiveLookAndFeel);
            DevExpress.Skins.SkinElement EvenRowSkin = GridSkin[DevExpress.Skins.GridSkins.SkinGridEvenRow];
            DevExpress.Skins.SkinElement OddRowSkin = GridSkin[DevExpress.Skins.GridSkins.SkinGridOddRow];
            //oddColour = OddRowSkin.Color.BackColor;
            oddColour = EvenRowSkin.Color.BackColor;

            gvAksBased.OptionsView.EnableAppearanceEvenRow = gvAksBased.OptionsView.EnableAppearanceOddRow = false;

            gvAksBased.RowCellClick += gvAksBased_RowCellClick;
            gvAksBased.RowStyle += gvAksBased_RowStyle;

            btnUpdate.ItemClick += btnUpdate_ItemClick;
        }

        Color oddColour;
        void gvAksBased_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
                if ((sender as GridView).GetRowCellValue(e.RowHandle, "StateId") != null)
                    if (intParse((sender as GridView).GetRowCellValue(e.RowHandle, "StateId")) == 4)
                        e.Appearance.BackColor = Color.LightPink;
                    else
                    {
                        if (e.RowHandle % 2 == 0)
                            e.Appearance.BackColor = oddColour;
                        else
                            e.Appearance.BackColor = Color.White;
                    }
        }

        void gvAksBased_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            PasifOnay.RowCellClick(gvAksBased, e);
        }

        private void AddNote_Click(object sender, EventArgs e)
        {
            string aksIds = gvAksBased.GetSelectedValues("AksesuarId");
            if (aksIds != String.Empty)
            {
                int stateId = 0;
                using (AksesuarNotuEkle ane = new AksesuarNotuEkle())
                    if (ane.ShowDialog() == DialogResult.OK)
                    {
                        stateId = ane.isPassive ? 5 : 0;
                        //if (ane.isPassive)
                        //    stateId = ane.isUsable ? 6 : 5;
                        //else
                        //    stateId = 0;
                        ExecuteNonQuery("InsAksesuarPasif", aksIds, WhoAmI, stateId, ane.isUsable, ane.note,ane.musteriFaturaDurumu,ane.faturaNo);

                        gvAksBased.RefreshDataSource("GetHareketsizAksesuar", WhoAmI);
                        gvAksBased.BestFitColumns();
                    }
            }
        }

        void btnUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAksBased.EndEditing();
            gvAksBased.RefreshDataSource("GetHareketsizAksesuar", WhoAmI);
            gvAksBased.BestFitColumns();
        }

        private void gvAksBased_DoubleClick(object sender, EventArgs e)
        {
            int handle = gvAksBased.FocusedRowHandle;

            if (handle > -1)
            {
                int AksesuarId = intParse(gvAksBased.GetRowCellValue(handle, "AksesuarId"));
                using (AksesuarHareketGC ah = new AksesuarHareketGC(AksesuarId, null, null))
                    ah.ShowDialog();
            }
        }

        //List<int> aksId;
        //private string GetAksIds()
        //{
        //    aksId = new List<int>();
        //    foreach (int rowHandle in gvAksBased.GetSelectedRows())
        //        if (rowHandle > -1)
        //            aksId.Add(gvAksBased.GetRowCellValue(rowHandle, "AksesuarId").ToInt());

        //    return string.Join(",", aksId.ToArray());
        //}
    }
}